package com.itheima.health.controller;

import com.itheima.health.entity.Result;
import com.itheima.health.service.OrderService;
import com.itheima.health.service.SetmealService;
import com.itheima.health.utils.redis.RedisUtil;
import com.itheima.health.utils.resources.RedisMessageConstant;
import com.itheima.health.utils.sms.SmsUtils;
import com.itheima.health.utils.sms.ValidateCodeUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.ValidationUtils;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

//  ordersetting/importOrderSettings
@RestController
@RequestMapping("order")
@Api(tags = "传智健康预约模块之套餐管理")
public class OrderController {

    @Reference
    private OrderService orderService;

    @Autowired
    private RabbitTemplate rabbitTemplate;


    @GetMapping("sendCodeToYourTelephone/{telephone}")
    @ApiOperation(value = "查询功能",notes = "检查项查询列表信息")
    public Result   sendCodeToYourTelephone(@PathVariable("telephone")String  telephone){
        //   生成随机验证码   发给用户手机
        String code = ValidateCodeUtils.generateValidateCode(4);
        //  aliyun 工具类   测试 开发
//        SmsUtils.validUserTelephone(telephone,code); //   真实发送短信   上线 代码 执行
        //  90s  验证码 存放redis
        //  投递到队列上
        Map map = new HashMap();
        map.put("telephone",telephone);
        map.put("code",code);
        rabbitTemplate.convertAndSend("health-queue",map);

        RedisUtil.set(RedisMessageConstant.SENDTYPE_ORDER+telephone,code,90, TimeUnit.SECONDS);
        return  new Result(code);
    }
    @PostMapping("add")
    @ApiOperation(value = "查询功能",notes = "检查项查询列表信息")
    public Result   add(@RequestBody Map map){
        //  下单操作
        return  new Result(orderService.add(map));
    }


    @GetMapping("findOrderInfoByOrderId/{oid}")
    @ApiOperation(value = "查询功能",notes = "检查项查询列表信息")
    public Result   findOrderInfoByOrderId(@PathVariable("oid") int oid){
        return  new Result(orderService.findOrderInfoByOrderId(oid));
    }






}
